import { ElMessageBox } from 'element-plus'

// 检查重复属性、属性值不能为空
export const checkDuplicateAttributes = (list) => {
  const attributeMap = new Map();
  for (const item of list) {
    const attributes = item.sku_specification.attributes;
    const attributeKey = JSON.stringify(attributes);

    // 检查所有属性值都存在且不为空
    const allValuesNotEmpty = Object.entries(attributes).every(
      ([key, value]) => key == "sku" || (value !== "" && value !== undefined)
    );
    
    if (!allValuesNotEmpty) {
      return { nullValue: true };
    }
    if (attributeMap.has(attributeKey)) {
      const duplicateIndex = attributeMap.get(attributeKey);
      return { duplicate: true, rowIndex: duplicateIndex + 1, attributes: attributes };
    } else {
      attributeMap.set(attributeKey, item.id - 1);
    }
  }
  return { duplicate: false };
};

// 二次确认
export const handletTwiceConfirm = (message) =>{
  return new Promise((resolve, reject) => {
    ElMessageBox.confirm(message, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(async () => {
        resolve(true)
      })
      .catch(() => {
        reject(false)
      });
  })
}

export const handleRegionSave = () =>{
    
}